#!/usr/bin/env python
# Physics module

# set g
g = 9.81

# initial variables
t = 0
s = 0

# variables which can be changed by the corresponding functions below in case needed in game
e_kin = 0
e_z = 0
e_s = 0

average_speed = 0

# functions
def fall_speed(h, timestep):
    """calculates the fall speed of object falling from h meters, timestep 0.05 seconds. """
    global t, s
    while h > 0:
        t += timestep
        s = 0
        s = 0.5 * (g * (t ** 2))
        h -= s
        if h < 0:
            h = 0
        print t, s, h
    t = 0
    s = 0

def e_kin(m, v):
    """Calculates kinetic energy of object based on speed and mass"""
    global e_kin
    e_kin = 0.5 * (m * (v ** 2))
    print e_kin

def reset_e_kin():
    """Resets global e_kin so that value can be calculated for another object"""
    global e_kin
    e_kin = 0

def e_z(m, h):
    """Calculates mass energy of object based on height, mass, and g"""
    global e_z
    e_z = m * g * h
    print e_z
	
def reset_e_z():
    """Resets global e_z so that value can be calculated for another object"""
    global e_z
    e_z = 0

def e_s(C, u):
	"""Calculates spring energy of object based on C and u"""
	global e_s
	e_s = 0.5 * (C * (u ** 2))
	print e_s
	
def reset_e_s():
	"""Resets global e_s so that value can be calculated for another object"""
	global e_s
	e_s = 0

def average_speed(d_s, d_t):
	"""Calculate average speed for a certain traject"""
	global average_speed
	average_speed = d_s / d_t
	print average_speed
	
def reset_average_speed():
	"""Resets global average_speed so that value can be calculated for another trajectory"""
	global average_speed
	average_speed = 0
